﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CollegeShop.Api.BaseComponent;
using CollegeShop.Api.ViewObject.Commodity;
using CollegeShop.Api.Infrastructure;
using CollegeShop.Api.Framework;
using System.Data;
using CollegeShop.Api.Entity;
using System.IO;
using System.Web;

namespace CollegeShop.Api.Commodity
{
    public class CommodityBiz : IComponent
    {
        private CommodityBiz() { }
        public static CommodityBiz Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (key)
                    {
                        if (instance == null)
                        {
                            instance = new CommodityBiz();
                        }
                    }
                }
                return instance as CommodityBiz;
            }
        }

        public List<CommodityVO> GetCommodities(int college, int category, int page, int pageSize)
        {
            return WithCache("CommodityBiz.GetCommodities_" + category,
                () =>
                {
                    var ds = new T_GoodsDal().GetList(college, "GoodsCategoryID = " + category + " and state = 1 limit " + (page * pageSize).ToString() + ", " + (pageSize - 1).ToString());
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        List<CommodityVO> list = new List<CommodityVO>();
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            try
                            {
                                CommodityVO item = new CommodityVO();
                                item.Id = row["ID"].ToInt32();
                                item.Code = row["GoodsCode"].ToString();
                                item.Name = row["GoodsName"].ToString();
                                item.Title = row["GoodsTitel"].ToString();
                                item.Type = row["GoodsCategoryID"].ToInt32();
                                if (item.Type > 0)
                                {
                                    item.TypeName = GetCommodityType(college)[item.Type].Name;
                                }
                                item.Description = row["GoodsDescription"].ToString();
                                item.Images = HttpContext.Current.Server.MapPath(row["Path"].ToString());
                                item.ResourceID = Convert.ToInt32(row["ResourceID"]);
                                item.Price = row["DefaultSalePrice"].ToString();
                                item.Standard = row["Standard"].ToString();
                                item.Unit = row["Unit"].ToString();
                                item.PackStandard = Convert.ToInt32(row["PackStandard"] == null || string.IsNullOrEmpty(row["PackStandard"].ToString()) ? "0" : row["PackStandard"].ToString());
                                item.NamePY = row["GoodsNamePY"].ToString();                               

                                list.Add(item);
                            }
                            catch { }
                        }
                        return list;
                    }
                    return null;
                }, 600);
        }
        public Dictionary<int, CommodityTypeVO> GetCommodityType(int college)
        {
            return WithCache<Dictionary<int, CommodityTypeVO>>("CommodityBiz.GetCommodityType",
                () =>
                {
                    var ds = new T_GoodscategoryDal().GetList(college, "parentid = 0");
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        Dictionary<int, CommodityTypeVO> dict = new Dictionary<int, CommodityTypeVO>();
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            try
                            {
                                int id = row["ID"].ToInt32();
                                if (!dict.ContainsKey(id))
                                {
                                    CommodityTypeVO item = new CommodityTypeVO();
                                    item.Id = row["ID"].ToInt32();
                                    item.Name = row["CategoryName"].ToString();
                                    dict.Add(id, item);
                                }
                            }
                            catch { }
                        }
                        return dict;
                    }
                    return null;
                }, 6000);
        }

        public CommodityVO GetCommodity(int college, int id)
        {
            return WithCache("CommodityBiz.GetCommodity_" + id, () =>
            {
                var goods = new T_GoodsDal().GetModel(college, id);
                if (goods != null)
                {
                    CommodityVO item = new CommodityVO();
                    item.Id = goods.ID;
                    item.Code = goods.GoodsCode;
                    item.Description = goods.GoodsDescription;
                    item.Images = goods.GoodsImage;
                    item.Name = goods.GoodsName;
                    item.Price = goods.DefaultSalePrice.ToString();
                    item.Standard = goods.Standard;
                    item.Title = goods.GoodsTitel;
                    item.Type = goods.GoodsCategoryID;
                    return item;
                }
                return null;
            }, 600);
        }

        #region 增、改、删 --三层动软生成代码调用
        /// <summary>
        /// 新增和修改商品对象
        /// </summary>
        /// <param name="commodityVO"></param>
        /// <returns></returns>
        public bool SaveCommodity(int college, CommodityVO commodityVO)
        {
            //转换图片
            string path = ConfigHelper.GetConfigString("ImgUrl") + commodityVO.Code + "-main.jpg";
            commodityVO.Images = path;
            //上传图片到服务器
            if (!ImageHelper.UploadImage(commodityVO.ImagesUrl, path))
            {
                return false;
            }
            T_Goods goods = ConvertGoodsVOToModel(commodityVO);
            if (commodityVO.Id != -1)
            { //修改               
                return new T_GoodsDal().Update(college,1, goods);
            }
            else
            { //新增
                return new T_GoodsDal().Add(college,1, goods);
            }
        }
        /// <summary>
        /// 删除商品信息
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public bool DelCommodity(int college, string ids, string idResources)
        {
            return new T_GoodsDal().DeleteList(college, ids, idResources);
        }
        #endregion

        #region VO转换为Model
        /// <summary>
        /// 将VO对象转换为Model对象
        /// </summary>
        /// <param name="commodityVO"></param>
        /// <returns></returns>
        private T_Goods ConvertGoodsVOToModel(CommodityVO commodityVO)
        {
            T_Goods goods = new T_Goods();
            goods.DefaultSalePrice = Convert.ToDecimal(string.IsNullOrEmpty(commodityVO.Price) ? "0.00" : commodityVO.Price);
            goods.GoodsCategoryID = commodityVO.Type;
            goods.GoodsCode = commodityVO.Code;
            goods.GoodsImage = commodityVO.Images;
            goods.GoodsName = commodityVO.Name;
            goods.GoodsNamePY = commodityVO.NamePY;
            goods.GoodsTitel = commodityVO.Title;
            goods.ID = commodityVO.Id;
            goods.PackStandard = commodityVO.PackStandard;
            goods.Standard = commodityVO.Standard;
            goods.State = commodityVO.State;
            goods.Unit = commodityVO.Unit;
            goods.GoodsImage = commodityVO.Images;
            goods.GoodsDescription = commodityVO.Description;
            return goods;
        }
        #endregion
    }
}
